package com.drivemanage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.drivemanage.dao.ExpDao;
import com.drivemanage.dao.OrderDao;
import com.drivemanage.entry.DriveResult;
import com.drivemanage.entry.ExpInfo;
import com.drivemanage.entry.OrderInfo;
import com.drivemanage.utils.OpenUtils;

@Service
@Transactional
public class ExpInfoServiceImpl implements ExpInfoService{

	@Resource
	private OrderDao orderdao;
	
	@Resource
	private ExpDao expdao;

	/**
	 * 查看试驾体验
	 */
	@Override
	public DriveResult showTestList(String testTag, String userid) {
		List<Map> driveTestList=new ArrayList<Map>();
		if(OpenUtils.isEmpty(testTag)||"默认排序".equals(testTag)){
			driveTestList=expdao.qryDriveTest();
		}
		if("最新发布".equals(testTag)){
			driveTestList=expdao.qryDriveTestByTime();
		}
		if("我的发布".equals(testTag)){
			driveTestList=expdao.qryMyDriveTest();
		}
		return new DriveResult(DriveResult.SUCCESS_CODE,"查询成功",driveTestList);
	}

	@Override
	public DriveResult writeArt(String title, String role, String orderid) {
		//1.获取订单信息
		OrderInfo orderinfo=orderdao.getOrderInfo(orderid);
		String car_id=orderinfo.getCar_id();
		//2.获取当前时间
		String time=OpenUtils.getSysDate().substring(0, 8);
		String article_time=OpenUtils.TransDate(time, "yyyyMMdd", "yyyy-MM-dd");
		//3.获取试驾体验id
		String art_id=OpenUtils.createTravelID();
		//4.入库
		ExpInfo expinfo=new ExpInfo(art_id, title, role, orderid, car_id, article_time, "", "");
		expdao.addArt(expinfo);
		//5.改变订单状态
		Map param=new HashMap();
		param.put("orderid", orderid);
		param.put("status", "3");
		orderdao.setstatus(param);
		return new DriveResult(DriveResult.SUCCESS_CODE,"发表成功","");
	}

	@Override
	public DriveResult testDetail(String artid) {
		List<Map> testList=expdao.getTestDetail(artid);
		Map test=testList.get(0);
		return new DriveResult(DriveResult.SUCCESS_CODE,"查询成功",test);
	}
}
